package com.guigutool.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigutool.reggie.dto.DishDto;
import com.guigutool.reggie.entity.Dish;
import com.guigutool.reggie.entity.DishFlavor;
import com.guigutool.reggie.mapper.DishMapper;
import com.guigutool.reggie.service.DishFlavorService;
import com.guigutool.reggie.service.DishService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: KingWang
 * @Date: 2023/3/12
 * @Desc:
 **/
@Service
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品，同时保存对应的口味数据
     * @param dishDto
     */
    @Override
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {

        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);
        Long dishId = dishDto.getId(); //菜品id
        //保存菜品的口味数据到菜品口味表dish_flavor中
        List<DishFlavor> dishFlavors = dishDto.getFlavors().stream().map(x -> {
            x.setDishId(dishId);
            return x;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishFlavors);
        String key = "dish:" + dishDto.getCategoryId();
        redisTemplate.delete(key);
    }

    @Transactional
    @Override
    public void updateWithFlavor(DishDto dishDto) {
        //保存菜品的基本信息到菜品表dish
        this.updateById(dishDto);
        Long dishId = dishDto.getId(); //菜品id
        //先清理dish_flavor表中记录
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dishId);
        dishFlavorService.remove(queryWrapper);

        //再保存菜品的口味数据到菜品口味表dish_flavor中
        List<DishFlavor> dishFlavors = dishDto.getFlavors().stream().map(x -> {
            x.setDishId(dishId);
            return x;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(dishFlavors);
        String key = "dish:" + dishDto.getCategoryId();
        redisTemplate.delete(key);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        //查询菜品基本信息，从dish查询
        Dish dish = super.getById(id);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);

        //查询菜品口味信息，从dish_flavor查询
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(queryWrapper);
        dishDto.setFlavors(flavors);
        return dishDto;
    }
}
